package net.y2t72.microblog.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.jstl.sql.Result;

import net.y2t72.microblog.bean.Member;
import net.y2t72.microblog.dao.MemberDao;
import net.y2t72.microblog.util.StringUtil;

/**
 * 会员表数据访问实现类
 * 
 * @author
 * 
 */

public class MemberDaoImpl implements MemberDao {
	Member member = null;

	/**
	 * 根据用户名查询用户信息
	 * 
	 * @param userName
	 *            会员邮箱
	 * @return 会员对象
	 * @author 任小斌
	 */
	public Member findMember(String email) {

		// 创建会员对象
		Member member = null;

		// 拼写用户查询语句
		String sql = "select email from Member where userName='" + email + "'";

		// 执行SQL语句
		Result result = BaseDao.getInstance().executeQuery(sql, null);

		// 处理结果
		if (result.getRowCount() > 0) {

			member = new Member(); // 实例化会员对象

			@SuppressWarnings("rawtypes")
			// 创建Map对象
			Map map = result.getRows()[0];

			member.setId(StringUtil.parseInt(StringUtil.parseString(map
					.get("id"))));

			member.setUsername(StringUtil.parseString(map.get("userName")));

			member.setNickname(StringUtil.parseString(map.get("nickName")));

			member.setPassword(StringUtil.parseString(map.get("password")));

			member.setSafetyCode(StringUtil.parseString(map.get("safetyCode")));
			;

			member.setGender(StringUtil.parseString(map.get("gender")));

			member.setEmail(StringUtil.parseString(map.get("email")));

			member.setIntroduction(StringUtil.parseString(map
					.get("introduction")));

			member.setDistrict(StringUtil.parseString(map.get("district")));

			member.setFaceUrl(StringUtil.parseString(map.get("faceUrl")));

			member.setRegdate(StringUtil.parseString(map.get("regdate")));

			member.setPageViews(StringUtil.parseInt(StringUtil.parseString(map
					.get("pageVisit"))));

			member.setLastvisit(StringUtil.parseString(map.get("lastvisit")));

			member.setLastactivity(StringUtil.parseString(map
					.get("lastactivity")));

			member.setOltime(StringUtil.parseInt(StringUtil.parseString(map
					.get("oltime"))));

			member.setLoginCount(StringUtil.parseInt(StringUtil.parseString(map
					.get("loginCount"))));

			member.setTopicCount(StringUtil.parseInt(StringUtil
					.parseString("topicCount")));

			member.setFollowCount(StringUtil.parseInt(StringUtil
					.parseString(map.get("followCount"))));

			member.setFansCount(StringUtil.parseInt(StringUtil.parseString(map
					.get("fansCount"))));

			member.setLastContent(StringUtil.parseString(map.get("lastContent")));

			member.setLastContTime(StringUtil.parseString(map
					.get("lastContTime")));

			member.setMsgCount(StringUtil.parseInt(StringUtil.parseString(map
					.get("msgCount"))));

			member.setUnreadMsgCount(StringUtil.parseInt(StringUtil
					.parseString(map.get("unreadMsgCount"))));

			member.setNewFollowCount(StringUtil.parseInt(StringUtil
					.parseString(map.get("newFollowCount"))));

			member.setMentionCount(StringUtil.parseInt(StringUtil
					.parseString(map.get("mentionCount"))));

			member.setCommentCount(StringUtil.parseInt(StringUtil
					.parseString(map.get("commentCount"))));

			member.setThemeBgcolor(StringUtil.parseString(map
					.get("theme_bgcolor")));

			member.setThemePicType(StringUtil.parseString(map
					.get("theme_pictype")));

			member.setThemeText(StringUtil.parseString(map.get("theme_text")));

			member.setThemeLink(StringUtil.parseString(map.get("theme_link")));

			member.setThemeSidebar(StringUtil.parseString(map
					.get("theme_sidebar")));

			member.setThemeSidebox(StringUtil.parseString(map
					.get("theme_sidebox")));

			member.setThemeBgurl(StringUtil.parseString(map.get("theme_bgurl")));
		}
		// 返回会员对象
		return member;
	}

	/**
	 * 添加用户信息
	 * 
	 * @param member
	 *            会员对象
	 * @return 影响的行数
	 * @author 任小斌
	 */
	public int addMember(Member member) {

		// 拼写添加用户SQL语句
		String sql = "insert into Member valuse(member_seq.nextval,?,?,?,?,default,?,?,?,?,default,default,?,?,default,default,default,default,default,?,?,default,default,default,default,default,?,?,?,?,?,?,?)";

		// 创建Object集合，设置用户参数
		Object[] param = { member.getUsername(), member.getNickname(),
				member.getPassword(), member.getSafetyCode(),
				member.getEmail(), member.getIntroduction(),
				member.getDistrict(), member.getFaceUrl(),
				member.getLastvisit(), member.getLastactivity(),
				member.getLastContent(), member.getLastContTime(),
				member.getThemeBgcolor(), member.getThemePicType(),
				member.getThemeText(), member.getThemeLink(),
				member.getThemeSidebar(), member.getThemeSidebox(),
				member.getThemeBgurl() };

		// 返回会员注册影响的行数
		return BaseDao.getInstance().executeUpdate(sql, param);
	}

	@Override
	/**
	 * 根据id查询用户信息
	 */
	public Member SeachMember(int memId) {
		String sql = "select * from Member where id=" + memId;
		Member member = new Member();
		Result result = BaseDao.getInstance().executeQuery(sql, null);
		if (result.getRowCount() > 0) {
			@SuppressWarnings("rawtypes")
			Map map = result.getRows()[0];
			member.setNickname(StringUtil.parseString(map.get("nickname"))); // 昵称
			member.setUsername(StringUtil.parseString(map.get("username"))); // 用户名
			member.setPassword(StringUtil.parseString(map.get("password"))); // 密码
			member.setId(StringUtil.parseInt(StringUtil.parseString(map
					.get("id")))); // id
			member.setDistrict(StringUtil.parseString(map.get("district"))); // 住址
			member.setGender(StringUtil.parseString(map.get("gender"))); // 性别
			member.setIntroduction(StringUtil.parseString(map
					.get("introduction"))); // 简介
			member.setFaceUrl(StringUtil.parseString(map.get("faceUrl"))); // 照片地址
		}
		return member;
	}

	@Override
	public int updateMember(Member member, String state) {
		String sql = null;
		if (state == "jiben") {
			sql = "update Member set nickname='" + member.getNickname()
					+ "',district='" + member.getDistrict() + "',gender='"
					+ member.getGender() + "',introduction='"
					+ member.getIntroduction() + "' where id=" + member.getId();
		} else if (state == "mima") {
			sql = "update Member set password='" + member.getPassword()
					+ "' where id=" + member.getId();
		} else {
			sql = "update Member set district='" + member.getDistrict()
					+ "' where id=" + member.getId();
		}

		int count = BaseDao.getInstance().executeUpdate(sql, null);
		return count;
	}

	/**
	 * 根据ID查询用户信息
	 * 
	 * @param id
	 * @return
	 * @author dp
	 */
	public List<Member> getMemberAll(int id, int pageSize, int curPage,
			boolean yn) {
		List<Member> list = new ArrayList<Member>();
		String sql = "";
		if (yn) {
			sql = "select id,nickName,followCount,fansCount,topicCount from "
					+ "(select id,nickName,followCount,fansCount,topicCount,rownum rn from Member "
					+ "where id in (select mid from buddy where bubbyId = "
					+ id + ")" + " and rownum < " + (curPage * pageSize + 1)
					+ " order by lastContTime desc)" + " where rn > "
					+ ((curPage - 1) * pageSize);
		} else {
			sql = "select id,nickName,followCount,fansCount,topicCount from "
					+ "(select id,nickName,followCount,fansCount,topicCount,rownum rn from Member "
					+ "where id in (select bubbyId from buddy where mid = "
					+ id + ")" + " and rownum < " + (curPage * pageSize + 1)
					+ " order by lastContTime desc)" + " where rn > "
					+ ((curPage - 1) * pageSize);
		}
		Result result = BaseDao.getInstance().executeQuery(sql, null);
		for (int i = 0; i < result.getRowCount(); i++) {
			member = new Member();
			@SuppressWarnings("rawtypes")
			Map map = result.getRows()[i];
			member.setId(StringUtil.parseInt(map.get("id")));
			member.setNickname(StringUtil.parseString(map.get("nickName")));
			member.setFollowCount(StringUtil.parseInt(map.get("followCount")));
			member.setFansCount(StringUtil.parseInt(map.get("fansCount")));
			member.setTopicCount(StringUtil.parseInt(map.get("topicCount")));
			list.add(member);
		}
		return list;
	}

	/**
	 * 根据条件查询广播
	 * 
	 * @param lastContent
	 * @return
	 * @author dp
	 */
	public List<Member> getMemberLastContent(String lastContent) {
		List<Member> list = new ArrayList<Member>();
		String sql = "select id,nickName,lastContent from Member where lastContent = '%"
				+ lastContent + "%'";
		Result result = BaseDao.getInstance().executeQuery(sql, null);
		for (int i = 0; i < result.getRowCount(); i++) {
			member = new Member();
			@SuppressWarnings("rawtypes")
			Map map = result.getRows()[i];
			member.setId(StringUtil.parseInt(map.get("id")));
			member.setNickname(StringUtil.parseString(map.get("nickName")));
			member.setLastContent(StringUtil.parseString(map.get("lastContent")));
			list.add(member);
		}
		return list;
	}

	/**
	 * 根据条件查询用户
	 * 
	 * @param lastContent
	 * @return
	 * @author dp
	 */
	public List<Member> getMemberNickName(String nickName) {
		List<Member> list = new ArrayList<Member>();
		String sql = "select id,nickName,followCount,fansCount,topicCount from Member where nickName = '"
				+ nickName + "'";
		Result result = BaseDao.getInstance().executeQuery(sql, null);
		for (int i = 0; i < result.getRowCount(); i++) {
			member = new Member();
			@SuppressWarnings("rawtypes")
			Map map = result.getRows()[i];
			member.setId(StringUtil.parseInt(map.get("id")));
			member.setNickname(StringUtil.parseString(map.get("nickName")));
			member.setFollowCount(StringUtil.parseInt(map.get("followCount")));
			member.setFansCount(StringUtil.parseInt(map.get("fansCount")));
			member.setTopicCount(StringUtil.parseInt(map.get("topicCount")));
			list.add(member);
		}
		return list;
	}
}
